package com.globallogic.week3.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.globallogic.week3.util.AuthUserHandler;
import com.globallogic.week3.util.MD5Util;
import com.globallogic.week3.util.UserHandler;
import com.globallogic.week3.util.Validator;

@WebServlet(urlPatterns="/register")
public class RegisterServlet extends HttpServlet {
	
	private static final Logger log = Logger.getLogger(RegisterServlet.class);
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.getRequestDispatcher("registration.jsp").forward(req, resp);
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		log.info("Start of user registration...");
		boolean operation = true;
		UserHandler handler = (UserHandler) req.getServletContext().getAttribute("handler");
		
		AuthUserHandler authUserHandler = (AuthUserHandler) req.getServletContext().getAttribute("authUserHandler");
		
		String login = req.getParameter("login");
		String pass = req.getParameter("pass");
		String conPass = req.getParameter("conPass");
		
		if (!Validator.validateLogin(login)){
			req.setAttribute("loginError", "Login is required 3-15 characters and '-' '_'");
			operation = false;
			log.warn("User login doesn't match to the pattern.");
		}
		if (!Validator.validatePass(pass)){
			req.setAttribute("passError", "Password mast contains digits,"
					+ "and  total size 6-20 characters.");
			operation = false;
			log.warn("User password doesn't match to the pattern.");
		}
		if (!Validator.comparePasswords(pass, conPass) || !Validator.validatePass(pass)){
			req.setAttribute("conPassError", "Passwords are not match");
			operation = false;
			log.warn("Confirmed password and password are different");
		}
		if (!operation){
			req.getRequestDispatcher("registration.jsp").forward(req, resp);
		} 
		if(handler.containsLogin(login)){
			req.setAttribute("loginError", "Such user is exist");
			req.getRequestDispatcher("registration.jsp").forward(req, resp);
			log.warn("Such login is alredy exist.");
		}
		
		if (Validator.validateLogin(login) && Validator.validatePass(pass)
				&& Validator.comparePasswords(pass, conPass) &&!handler.containsLogin(login)){
			handler.add(login, MD5Util.encode(pass));
			req.setAttribute("operationStatus", "Registration is successfull.");
			
			String sessionId = req.getSession().getId();
			log.error("SessionId: "+sessionId);
			authUserHandler.add(sessionId);
			req.getSession().setAttribute("isAuth", sessionId);
			log.info("User registration is finished successfull.");
			req.getRequestDispatcher("notify.jsp").forward(req, resp);
		}
		
		
	}
}
